<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Language_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $language ="";
	var $code ="";
	var $CI;
	
	function __construct() {
		$this->CI = &get_instance();
		$this->current_dbprefix = $this->CI->db->dbprefix;
		$this->CI->db->set_dbprefix('default_');
	}
	function __destruct() {
		$this->CI->db->set_dbprefix($this->current_dbprefix);
	}
	
	function setId($input){
		$this->id=$input;
	}
	function setLanguage($input){
		$this->language=$input;
	}
	function setCode($input){
		$this->code =$input;
	}
	function getId(){
		return $this->id;
	}
	function getLanguage(){
		return $this->language;
	}
	function getCode(){
		return $this->code;
	}
	
	function init($cmd, $input)
	{
		$this->CI->db->set_dbprefix('default_');
		if($cmd == 'id'){
			$result = $this->CI->db->where('id',$input)->get(TBL_LANGUAGE)->result();
		}else if($cmd == 'language'){
			$result = $this->CI->db->where('language',$input)->get(TBL_LANGUAGE)->result();
		}else if($cmd == 'code'){
			$result = $this->CI->db->where('code',$input)->get(TBL_LANGUAGE)->result();
		}else{
			return false;
		}
		if( !empty($result) ){
			$this->id = $result[0]->id;
			$this->language = $result[0]->language;
			$this->code = $result[0]->code;
			return $this;
		}
		return false;
	}
	function getLanguageList(){
		$this->CI->db->set_dbprefix('default_');
		return $this->CI->db->get(TBL_LANGUAGE)->result();
	}
	function save($entity){
		$this->CI->db->set_dbprefix('default_');
		
		$this->CI->db->set('language',$entity->getLanguage());
		$this->CI->db->set('code',$entity->getCode());
		$this->CI->db->insert(TBL_LANGUAGE);
		return $this->CI->db->insert_id();
	}
	function update(){
		$this->CI->db->set_dbprefix('default_');
		
		$this->CI->db->set('language', $this->language);
		$this->CI->db->set('code', $this->code);
		return $this->CI->db->where('id', $this->id)->update(TBL_LANGUAGE);
	}
	function delete($id){
		$this->CI->db->set_dbprefix('default_');
		
		$this->CI->db->where('id',$id);
		$this->CI->db->delete(TBL_LANGUAGE);
	}
	function checkExistedName($id, $name){
		$this->CI->db->set_dbprefix('default_');
		
		if($id){
			$this->CI->db->where('id !=',$id);
		}
		$this->CI->db->where('language',$name);
		return $this->CI->db->get(TBL_LANGUAGE)->result();
	}
	
	/////////////////////////////////////////////////////////////////////////
	function getEscortLanguagesInfo($id){
		$this->CI->db->set_dbprefix('default_');
		
		$res = $this->CI->db->where('id',$id)->get('default_google_languages')->result();
		return ($res)?$res[0]:false;
	}
	
}